/**
 * Copyright (c) 2013, Robert Cherry * All rights reserved.
 *
 * This file is part of the Faust Editor.
 *
 * The Faust Editor is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * The Faust Editor is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * The Faust Editor. If not, see <http://www.gnu.org/licenses/>.
 */
package form;

import core.event.AnimationEvent;
import core.event.AnimationListener;
import core.world.Animation;
import javax.swing.SpinnerNumberModel;

/**
 *
 * @author mcherry
 */
public class AnimationViewer extends javax.swing.JDialog implements AnimationListener {

    // Variable Delcaration
    // Swing Classes
    private ImagePanel imageJPanel;
    // Project Classes
    private Animation animation;
    // Data Inst.
    private int frameIndex;
    private int milliseconds;
    // End of Variable Declaration

    public AnimationViewer(java.awt.Dialog parent, Animation animation, boolean modal) {
        super(parent, modal);
        initComponents();

        // We only want the copy not the original
        this.animation = animation;

        // Initialize some custom commands
        init();
    }

    private void init() {

        // Final call
        setAnimation(animation);

        // Custom paint override
        imageJPanel = new ImagePanel(mainJScrollPane);
        imageJPanel.setShowTextile(true);

        // Set the viewport as the imageJPanel
        mainJScrollPane.setViewportView(imageJPanel);

        // Quick checkaroo
        if (getParent() instanceof IllustrationEditor) {
            // Cast
            //final IllustrationEditor editor = (IllustrationEditor) getParent();
            //this.setLocation(editor.getX() - 32, editor.getY() - 32);
        }
    }

    private void refresh() {

        //
        if (animation != null) {

            // Kick start the animation to begin triggering animation events
            if (animation.isRunning() == false) {
                animation.start();
            }

            // Refresh this as well
            delayJSpinner.setValue(animation.getDelay());
        }

        //
        this.validate();
        this.repaint();
    }

    public Animation getAnimation() {
        return animation;
    }

    public int getDelay() {
        return (animation == null ? 0 : animation.getDelay());
    }

    public void setAnimation(Animation animation) {

        //
        if (animation != null) {

            // Grab copy of it
            animation = animation.reproduce();

            //
            animation.removeAnimationListener(this);
            animation.addAnimationListener(this);
            animation.setCycles(-1);

            // Draw the outline
            animation.setPaintShape(true);

            //
            milliseconds = animation.getDelay();

            //
            animation.start();

            //
            if (animation.length() > 0) {

                //
                final SpinnerNumberModel frameModel = new SpinnerNumberModel(0, 0, animation.length(), 1);
                final SpinnerNumberModel delayModel = new SpinnerNumberModel(milliseconds, 0, 10000, 1);

                //
                frameJSpinner.setModel(frameModel);
                delayJSpinner.setModel(delayModel);
            }

            //
            this.animation = animation;
            imageJPanel.updatePanel(animation);
        }

        //
        refresh();
    }

    public void setDelay(int ms) {

        //
        delayJSpinner.setValue(ms);

        //
        refresh();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        mainJScrollPane = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        resetJButton = new javax.swing.JButton();
        filler3 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        closeJButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        speedJLabel = new javax.swing.JLabel();
        delayJSpinner = new javax.swing.JSpinner();
        jPanel3 = new javax.swing.JPanel();
        frameJLabel = new javax.swing.JLabel();
        frameJSpinner = new javax.swing.JSpinner();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Viewing Animations");
        setMinimumSize(new java.awt.Dimension(290, 315));
        setPreferredSize(new java.awt.Dimension(298, 333));
        setResizable(false);

        mainJScrollPane.setMaximumSize(new java.awt.Dimension(271, 178));
        mainJScrollPane.setMinimumSize(new java.awt.Dimension(271, 178));
        mainJScrollPane.setPreferredSize(new java.awt.Dimension(271, 178));

        jPanel1.setMaximumSize(new java.awt.Dimension(32903, 26));
        jPanel1.setMinimumSize(new java.awt.Dimension(136, 26));
        jPanel1.setPreferredSize(new java.awt.Dimension(352, 26));
        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        resetJButton.setText("Reset");
        resetJButton.setMaximumSize(new java.awt.Dimension(68, 26));
        resetJButton.setMinimumSize(new java.awt.Dimension(68, 26));
        resetJButton.setPreferredSize(new java.awt.Dimension(68, 26));
        resetJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetJButtonActionPerformed(evt);
            }
        });
        jPanel1.add(resetJButton);
        jPanel1.add(filler3);

        closeJButton.setText("Close");
        closeJButton.setMaximumSize(new java.awt.Dimension(88, 26));
        closeJButton.setMinimumSize(new java.awt.Dimension(88, 26));
        closeJButton.setPreferredSize(new java.awt.Dimension(88, 26));
        closeJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeJButtonActionPerformed(evt);
            }
        });
        jPanel1.add(closeJButton);

        java.awt.GridBagLayout jPanel2Layout = new java.awt.GridBagLayout();
        jPanel2Layout.columnWidths = new int[] {0, 25, 0};
        jPanel2Layout.rowHeights = new int[] {0};
        jPanel2.setLayout(jPanel2Layout);

        speedJLabel.setText("Animation Delay (Millis):");
        speedJLabel.setMaximumSize(new java.awt.Dimension(174, 24));
        speedJLabel.setMinimumSize(new java.awt.Dimension(174, 24));
        speedJLabel.setPreferredSize(new java.awt.Dimension(174, 24));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel2.add(speedJLabel, gridBagConstraints);

        delayJSpinner.setMaximumSize(new java.awt.Dimension(72, 24));
        delayJSpinner.setMinimumSize(new java.awt.Dimension(72, 24));
        delayJSpinner.setPreferredSize(new java.awt.Dimension(72, 24));
        delayJSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                delayJSpinnerStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        jPanel2.add(delayJSpinner, gridBagConstraints);

        jPanel3.setMaximumSize(new java.awt.Dimension(246, 24));
        jPanel3.setMinimumSize(new java.awt.Dimension(246, 24));
        jPanel3.setPreferredSize(new java.awt.Dimension(246, 24));
        java.awt.GridBagLayout jPanel3Layout = new java.awt.GridBagLayout();
        jPanel3Layout.columnWidths = new int[] {0, 25, 0};
        jPanel3Layout.rowHeights = new int[] {0};
        jPanel3.setLayout(jPanel3Layout);

        frameJLabel.setText("Current Frame:");
        frameJLabel.setMaximumSize(new java.awt.Dimension(174, 24));
        frameJLabel.setMinimumSize(new java.awt.Dimension(174, 24));
        frameJLabel.setPreferredSize(new java.awt.Dimension(174, 24));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel3.add(frameJLabel, gridBagConstraints);

        frameJSpinner.setEnabled(false);
        frameJSpinner.setMaximumSize(new java.awt.Dimension(72, 24));
        frameJSpinner.setMinimumSize(new java.awt.Dimension(72, 24));
        frameJSpinner.setPreferredSize(new java.awt.Dimension(72, 24));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        jPanel3.add(frameJSpinner, gridBagConstraints);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(mainJScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainJScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeJButtonActionPerformed

        // Close this out
        setVisible(false);
    }//GEN-LAST:event_closeJButtonActionPerformed

    private void resetJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetJButtonActionPerformed

        // TODO add your handling code here:
        delayJSpinner.setValue(milliseconds);

        //
        //animation.start();
        animation.restart();
    }//GEN-LAST:event_resetJButtonActionPerformed

    private void delayJSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_delayJSpinnerStateChanged

        // TODO add your handling code here:
        final int ms = ((Number) delayJSpinner.getValue()).intValue();

        //
        animation.setDelay(ms);
        //animation.restart();
    }//GEN-LAST:event_delayJSpinnerStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeJButton;
    private javax.swing.JSpinner delayJSpinner;
    private javax.swing.Box.Filler filler3;
    private javax.swing.JLabel frameJLabel;
    private javax.swing.JSpinner frameJSpinner;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane mainJScrollPane;
    private javax.swing.JButton resetJButton;
    private javax.swing.JLabel speedJLabel;
    // End of variables declaration//GEN-END:variables

    @Override
    public void animationEnd(AnimationEvent event) {

        //
        if (event.getSource() == animation) {

            //
            frameIndex = animation.getIndex();

            //
            frameJSpinner.setValue(frameIndex);

            //
            imageJPanel.repaint();
        }
    }

    @Override
    public void animationStep(AnimationEvent event) {

        //
        if (event.getSource() == animation) {

            //
            frameIndex = animation.getIndex();

            //
            frameJSpinner.setValue(frameIndex);

            //
            imageJPanel.repaint();
        }
    }
}
